package org.example.exampreparationtreasure.exception;

import org.example.exampreparationtreasure.common.BusinessException;
import org.example.exampreparationtreasure.common.CommonResultStatus;
import org.example.exampreparationtreasure.common.ResultStatus;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {
    
    @ExceptionHandler(IncorrectQuestionException.class)
    public ResponseEntity<Map<String, Object>> handleIncorrectQuestionException(IncorrectQuestionException ex) {
        ResultStatus status = ex.getStatus();
        Map<String, Object> body = new HashMap<>();
        body.put("success", false);
        body.put("code", status.getCode());
        body.put("message", ex.getMessage());
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(body);
    }
    
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<Map<String, Object>> handleBusinessException(BusinessException ex) {
        ResultStatus status = ex.getStatus();
        Map<String, Object> body = new HashMap<>();
        body.put("success", false);
        body.put("code", status.getCode());
        body.put("message", ex.getMessage());
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(body);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Map<String, Object>> handleOtherException(Exception ex) {
        Map<String, Object> body = new HashMap<>();
        body.put("success", false);
        body.put("code", CommonResultStatus.SERVER_ERROR.getCode());
        body.put("message", CommonResultStatus.SERVER_ERROR.getMessage());
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(body);
    }
}


